<?php
  /*
   * 模块划分函数
   */
  function dealGetM($m){
	$path     = "";
	$class    = "";
	$function = "";
  	$mArray = explode("/",$m);
	$mArraySize = count($mArray);
	if(trim($mArray[$mArraySize-1])==""||trim($mArray[$mArraySize-1])==" "){
		$mArray[$mArraySize-1] = "Index";
	}
	if($mArraySize<=2){
		$path     = "";
		$class    = ucfirst(strtolower($mArray[0]));
		if($mArraySize==1)
		    $function = "Index";
		else
			$function = ucfirst($mArray[1]);
	}else {
		$class    = ucfirst(strtolower($mArray[$mArraySize-2]));
		$function = ucfirst($mArray[$mArraySize-1]);
		unset($mArray[$mArraySize-2]);
		unset($mArray[$mArraySize-1]);
		for($i=0;$i<$mArraySize-1;$i++){
			$mArray[$i] = ucfirst(strtolower($mArray[$i]));
		}
		$path = implode("/",$mArray);
	}
	return array("path"=>$path,"class"=>$class,"function"=>$function);
  }
  
  /*
   * 反注入函数
   */ 
  function inject_check($sql_str){
	   $check=eregi('select|insert|update|delete|\`|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);
	   if($check)
		   return false;
		return true;
	}
 
  
  
  /*
   * 路由函数
   */  
  function route($modArray,$valArray=null){
  	$pathArray      = explode("/",$modArray['path']);  //获取所有父类的路径
	$pathArraySize  = count($pathArray);      //父类个数+1
	$parentClassPath = $pathArray[0];
	for($i=1;$i<$pathArraySize;$i++){        //require所有父类
	    $parentClassFilepath = "./controler/".$parentClassPath.".php";  //父类文件路径
	    if(!file_exists($parentClassFilepath)){
	    	break;
	    }
		require_once($parentClassFilepath);
		$parentClassPath.="/".$pathArray[$i];
	}
	
	$template_dir = $modArray['path'].$modArray['class'];  //本类对应的模板调用目录
	
	$classPath = "./controler/".$modArray['path'].$modArray['class'].".php"; //本类文件路径
	if(file_exists($classPath)){//判断本类是否存在
		require_once($classPath); //require本类
		if(!method_exists($modArray['class'],$modArray['function'])){ //判断路由的函数是否存在
		    $modArray['class'] = DEFAULT_MOD;
			$modArray['function'] = DEFAULT_FUN;  //不存在就是用默认的模块函数
			$template_dir = "Index";
		}
	}else{//本类不存在就是用默认模块
	   $modArray['class'] = DEFAULT_MOD;  
	   $modArray['function'] = DEFAULT_FUN;
	   $template_dir = "Index";
	}
	

  	$valStr = "";//将用于作为类函数的参数转为相应的字符串
	if($valArray){
		$valStr=implode(",",$valArray);
		if(!inject_check($valStr)){  //若传递参数中含有敏感字符，则返回默认模块
			$valStr = "";
			$modArray['class'] = DEFAULT_MOD;  
	        $modArray['function'] = DEFAULT_FUN;
		}
	}
	
			
	eval(
	"\$class = new ".$modArray['class']."();
	 \$class->setTemplateDir(\"$template_dir\");
	 loadPlugIn(\$class->getPlugIn());
	 \$class->".$modArray['function']."(".$valStr.");
	"
	);
  }

  
  /*
   * 加载第三方插件函数
   */
  
  function loadPlugIn($fileArray){
  	if(!is_array($fileArray)){
  		return;
  	}
	foreach($fileArray as $val){
		$plugInFile = explode(".",$val);
		if(trim($plugInFile[count($plugInFile)-1])!="php"){
			echo "error plug in name ".$val;
			exit;
		}
		require_once($val);
	}
  }
  

  /**
   * 获取文件夹列表
   */
  function getFile($dir,$sign="file"){
  	$fileArray[]=NULL;
	if (false != ($handle = opendir ( $dir ))){
		$i=0;
		 while ( false !== ($file = readdir ( $handle )) ) {
		 	if($sign=="dir"){
			 	if ($file != "." && $file != ".."&&!strpos($file,".")) {
			 		$fileArray[$i]=$file;
					$i++;
				}
			}else if($sign=="file"){
				if ($file != "." && $file != ".."&&strpos($file,".")) {
			 		$fileArray[$i]=$file;
					$i++;
				}
			}
		 }
		 closedir ( $handle );
	}
	return $fileArray;
  }
  


?>